package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.domain.ZzxxAdmin;
import com.ruoyi.system.domain.ZzxxAdminCourse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.mapper.ZzxxAdminMapper;
import com.ruoyi.system.service.IZzxxAdminService;

/**
 * 管理员Service业务层处理
 * 
 * @author //
 * @date 2022-06-20
 */
@Service
public class ZzxxAdminServiceImpl implements IZzxxAdminService 
{
    @Autowired
    private ZzxxAdminMapper zzxxAdminMapper;

    /**
     * 查询管理员
     * 
     * @param adminId 管理员主键
     * @return 管理员
     */
    @Override
    public ZzxxAdmin selectZzxxAdminByAdminId(Long adminId)
    {
        return zzxxAdminMapper.selectZzxxAdminByAdminId(adminId);
    }

    /**
     * 查询管理员列表
     * 
     * @param zzxxAdmin 管理员
     * @return 管理员
     */
    @Override
    public List<ZzxxAdmin> selectZzxxAdminList(ZzxxAdmin zzxxAdmin)
    {
        return zzxxAdminMapper.selectZzxxAdminList(zzxxAdmin);
    }

    /**
     * 新增管理员
     * 
     * @param zzxxAdmin 管理员
     * @return 结果
     */
    @Transactional
    @Override
    public int insertZzxxAdmin(ZzxxAdmin zzxxAdmin)
    {
        int rows = zzxxAdminMapper.insertZzxxAdmin(zzxxAdmin);
        insertZzxxAdminCourse(zzxxAdmin);
        return rows;
    }

    /**
     * 修改管理员
     * 
     * @param zzxxAdmin 管理员
     * @return 结果
     */
    @Transactional
    @Override
    public int updateZzxxAdmin(ZzxxAdmin zzxxAdmin)
    {
        zzxxAdminMapper.deleteZzxxAdminCourseByAdminId(zzxxAdmin.getAdminId());
        insertZzxxAdminCourse(zzxxAdmin);
        return zzxxAdminMapper.updateZzxxAdmin(zzxxAdmin);
    }

    /**
     * 批量删除管理员
     * 
     * @param adminIds 需要删除的管理员主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZzxxAdminByAdminIds(Long[] adminIds)
    {
        zzxxAdminMapper.deleteZzxxAdminCourseByAdminIds(adminIds);
        return zzxxAdminMapper.deleteZzxxAdminByAdminIds(adminIds);
    }

    /**
     * 删除管理员信息
     * 
     * @param adminId 管理员主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZzxxAdminByAdminId(Long adminId)
    {
        zzxxAdminMapper.deleteZzxxAdminCourseByAdminId(adminId);
        return zzxxAdminMapper.deleteZzxxAdminByAdminId(adminId);
    }

    /**
     * 新增管理员管理课程信息
     * 
     * @param zzxxAdmin 管理员对象
     */
    public void insertZzxxAdminCourse(ZzxxAdmin zzxxAdmin)
    {
        List<ZzxxAdminCourse> zzxxAdminCourseList = zzxxAdmin.getZzxxAdminCourseList();
        Long adminId = zzxxAdmin.getAdminId();
        if (StringUtils.isNotNull(zzxxAdminCourseList))
        {
            List<ZzxxAdminCourse> list = new ArrayList<ZzxxAdminCourse>();
            for (ZzxxAdminCourse zzxxAdminCourse : zzxxAdminCourseList)
            {
                zzxxAdminCourse.setAdminId(adminId);
                list.add(zzxxAdminCourse);
            }
            if (list.size() > 0)
            {
                zzxxAdminMapper.batchZzxxAdminCourse(list);
            }
        }
    }
}
